<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/19/019
 * Time: 9:29
 */

namespace App\Http\Controllers\DepartmentManagement;

use App\Http\Controllers\CommonController;
use Request;
use App\Models\Admin;
use App\Models\Ipsell;
use Illuminate\Support\Facades\DB;
class ProductsManagementController extends CommonController
{
    protected  $pageNum=15;

    public function login()//显示管理员登录界面
    {
        return view('departmentManagement/products/login');
    }

    public function doLogin()//处理登录方法
    {
        $request= Request::all();
        $where_products = [
            "type" => 9,"password" => md5( $request[ 'password' ] ),
            "account" => $request[ 'account' ],
        ];
        $producter_model = Admin::where( $where_products )->first();
        if (!empty( $producter_model )){
            $producter    = $producter_model->toArray();
            $Session      = Session();
            $session_data = [ "product_id" => $producter[ 'id' ],"product_name" => "产品交易部-" . $producter[ 'real_name' ],
            ];
            $Session->put( "product",$session_data );

            return redirect()->route( 'management.productsTrading.IpSell' );

        }
        else{
            return redirect()->route( 'management.productsTrading.login' );
        }
    }

    public function IpSell()//显示ip出售界面方法
    {
        $Session = Session();
        $value=$Session->get('product');
//        dd($value);
        if ($value==null){
            return redirect()->route( 'management.productsTrading.login' );
        }
        $product=Ipsell::with(['FirstType','SecondType'])
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        //dump($product);exit;
        return view('departmentManagement/products/ipSell', compact($data));
    }

    public function IpSellProgress()//修改进度方法
    {
        $Session = Session();
        $value=$Session->get('product');
//        dd($value);
        if ($value==null){
            return redirect()->route( 'management.productsTrading.login' );
        }
        $input = Request::all();
        $id = $input['hide_id'];
//        dd($id);
        $data['progress'] = $input['edit_progress'];
        if (!empty($data)) {
            $res = DB::table('ipsell')->where(array("id" => $id))->update($data);
            if ($res) {
                return back()->with('success', '修改成功');
            } else {
                return back()->with('error', '修改失败');
            }
        } else {
            return back()->with('error', '暂无数据');
        }
    }

    public function IpSellCertain()//多条件筛选方法
    {
        $Session = Session();
        $value=$Session->get('product');
//        dd($value);
        if ($value==null){
            return redirect()->route( 'management.productsTrading.login' );
        }
        $request = Request::all();
        $first_type = $request['first_type'];
        $second_type = $request['second_type'];
        $name = $request['name'];
        $created_at = $request['created_at'];
        $progress = $request['progress'];
        $arr_province = ["不填","专利","商标","版权","项目"];
        $arr_second_type=config('config.arr_second_type');
        for ($j = 0;$j < sizeof($arr_second_type);$j++){
            if ($second_type == $arr_second_type[$j]){
                if ($j==0)
                    $j='';
                break;
            }
        }
        for ($i = 0;$i < 5;$i++){
            if ($first_type == $arr_province[$i]){
                if ($i==0)
                    $i='';
                break;
            }
        }
        // dd($request);
        $product=Ipsell::with(['FirstType','SecondType'])
            ->when($i, function ($query) use ($i) {
                return $query->where('first_type', $i);
            })
            ->when($j, function ($query) use ($j) {
                return $query->where('second_type', $j);
            })
            ->when($name, function ($query) use ($name) {
                return $query->where('product_name', $name);
            })
            ->when($created_at, function ($query) use ($created_at) {
                return $query->where('created_at','like','%'.$created_at.'%');
            })
            ->when($progress, function ($query) use ($progress) {
                return $query->where('progress', $progress);
            })
            ->paginate($this->pageNum);
        $first_type = object_to_array(DB::table('first_type')->get(['id', 'name']));
        $data = ['product','first_type'];
        return view('departmentManagement/products/ipSellCertain', compact($data));
    }

    public function loginOut()
    {
        $Session   = Session();
        $Session->remove('product');
        return redirect()->route( 'management.productsTrading.login' );
    }

}